package com.hhf.feiojcodesandbox.service.impl;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.dfa.WordTree;
import com.hhf.feiojcodesandbox.model.ExecuteCodeRequest;
import com.hhf.feiojcodesandbox.model.ExecuteCodeResponse;
import com.hhf.feiojcodesandbox.model.ExecuteMessage;
import com.hhf.feiojcodesandbox.model.JudgeInfo;
import com.hhf.feiojcodesandbox.service.CodeSandbox;
import com.hhf.feiojcodesandbox.service.CodeSandboxTemplate;
import com.hhf.feiojcodesandbox.utils.ProcessUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @Author hhf
 * @Date 2024/10/27
 * @Description Java原生代码沙箱
 */
@Slf4j
public class JavaNativeCodeSandbox extends CodeSandboxTemplate {

    public static void main(String[] args) {
        JavaNativeCodeSandbox javaNativeCodeSandbox = new JavaNativeCodeSandbox();

        ExecuteCodeRequest executeCodeRequest = new ExecuteCodeRequest();
        executeCodeRequest.setInputList(Arrays.asList("1 2", "1 3"));
//        String code = ResourceUtil.readStr("testCode/simpleComputeArgs/Add.java", StandardCharsets.UTF_8);
//        String code = ResourceUtil.readStr("testCode/unSafe/TimeExceed.java", StandardCharsets.UTF_8);
//        String code = ResourceUtil.readStr("testCode/unSafe/MemoryExceed.java", StandardCharsets.UTF_8);
        String code = ResourceUtil.readStr("testCode/unSafe/ReadFile.java", StandardCharsets.UTF_8);
//        String code = ResourceUtil.readStr("testCode/unSafe/WriteFile.java", StandardCharsets.UTF_8);
//        String code = ResourceUtil.readStr("testCode/unSafe/Exec.java", StandardCharsets.UTF_8);
        executeCodeRequest.setCode(code);
        executeCodeRequest.setLanguage("java");


        ExecuteCodeResponse response = javaNativeCodeSandbox.executeCode(executeCodeRequest);
        if (response == null) {
            log.error("发生错误");
        } else {
            log.info(response.toString());
        }
    }

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        return super.executeCode(executeCodeRequest);
    }

}
